package com.wzf.greattruth.test.zookeeper.elect;

import java.io.File;
import java.util.List;
import java.util.Random;

import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;

import com.wzf.greattruth.zookeeper.election.ElectionEventAware;
import com.wzf.greattruth.zookeeper.election.ElectionEventType;
import com.wzf.greattruth.zookeeper.election.ElectionHandler;

public class ZookeeperElectionTester {
    private static String HOST = "localhost:2181";
    private static int SESSION_TIMEOUT = 1000 * 60 * 60;

    protected static final List<ACL> acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;

    private static ZooKeeper zookeeper;

    public static void main(String[] args) throws Exception {
        zookeeper = new ZooKeeper(HOST, SESSION_TIMEOUT, null);

        ZookeeperElectionTester tester = new ZookeeperElectionTester();
        tester.testElection();

    }

    private void testElection() {
        int times = 5;
        final String electionDir = File.separator + "zk" + File.separator + "election";
        final String electionLockDir = File.separator + "zk" + File.separator + "electionlock";
        final String lockNodeKey = "lockNodeKey";
        final Random random = new Random();
        for (int i = 0; i < times; i++) {
            final int index = i;
            Thread t = new Thread(new Runnable() {

                @Override
                public void run() {
                    final String offerName = "offer-" + index;
                    ElectionEventAware<String> aware = new ElectionEventAware<String>() {
                        @Override
                        public void onElectionEvent(ElectionEventType electionEventType) {
                            System.err.println("---------------onElectionEvent offer="+this.getObject()+"  "+electionEventType);
                            
                        }
                    };
                    aware.setObject(offerName);
                    
                    ElectionHandler electionHandler = new ElectionHandler(zookeeper, electionDir, offerName,
                            electionLockDir, lockNodeKey, 5000, 3000);
                    electionHandler.addElectionEventAware(aware);
                    boolean start = electionHandler.start();
                    System.err.println(offerName + " start="+start);

                    
                    //不是leader的话一直等待
                    while( true ){
                        boolean isLeader = electionHandler.isLeader() ;
                        if( !isLeader ){
                            System.err.println(offerName + " is not leader, so sleep");
                            try {
                                Thread.sleep( 1000l);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }else{
                            break ;
                        }
                    }
                    
                    
                    int second = random.nextInt(10);
                    try {
                        Thread.sleep(second * 1000l);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    
                    boolean stop = electionHandler.stop() ;
                    System.err.println(offerName + " stop="+stop);
                }
            });
            t.start();
        }
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}
